package com.maycari.captainbi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maycari.captainbi.common.constant.CaptainBIConstant;
import com.maycari.captainbi.data.dto.Captain10Dto;
import com.maycari.captainbi.data.dto.Captain11Dto;
import com.maycari.captainbi.data.po.Captain10Po;
import com.maycari.captainbi.data.po.Captain11Po;
import com.maycari.captainbi.listener.SyncDto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.mapper.Captain11Dao;
import com.maycari.captainbi.service.Captain11Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maycari.captainbi.util.CaptainBIUtil;
import com.maycari.tool.utils.BeanUtil;
import com.maycari.tool.utils.CollectionUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺运营指数报告列表(客户服务) 服务实现类
 * </p>
 *
 * @author 郝伟
 * @since 2021-04-02
 */
@Service
@Slf4j
public class Captain11ServiceImpl extends ServiceImpl<Captain11Dao, Captain11Po> implements Captain11Service {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Override
    public List<Captain11Dto> queryListAll() {
        Date start_time = DateUtil.parse("2021-04-01 00:00:00", DateUtil.PATTERN_DATETIME);
        String result = new String();
        try {
            Date end_time = DateUtil.plusMonths(start_time, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            int page = 1;
            int page_size = 20;
/*
            String buhuo = "3";
*/
            TreeMap<String, String> params = new TreeMap<>();
/*
            params.put("buhuo", buhuo);
*/
            params.put("page", String.valueOf(page));
            params.put("page_size", String.valueOf(page_size));
            params.put("start_time", simpleDateFormat.format(start_time));
            params.put("end_time", simpleDateFormat.format(end_time));
            String action = "http://cronapi.captainbi.com/index.php?m=cronapi&c=goods&a=getAmazonStoreCustomerExperienceList";

            result = CaptainBIUtil.sendRequest(action, params);

            JSONObject jsonObject = JSON.parseObject(result);
            String data = jsonObject.getString("data");
            System.out.println(data);
            if (StringUtil.isNotBlank(data)) {
                List<Captain11Po> captain47Pos = JSON.parseArray(data, Captain11Po.class);
                for (int i = 0; i < captain47Pos.size(); i++) {
                    Captain11Po captain47Po = captain47Pos.get(i);

                    save(captain47Po);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean sync(Integer authorizationId, Date startTime, Date endTime) {

        SyncDto dto = new SyncDto();
        dto.setAuthorizationId(authorizationId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dto.setBeanId("captain11ServiceImpl");
        TreeMap<String, String> params = new TreeMap<>();
        params.put("start_time", simpleDateFormat.format(startTime));
        params.put("end_time", simpleDateFormat.format(endTime));
        dto.setUrl(CaptainBIConstant.CaptainBIUrl.url_11.value());
        dto.setParams(params);
        jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER, dto);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertBatch(JSONArray jsonArray) {
        log.info("##########################################---11----批量保存------");
        List<Captain11Po> addList = new ArrayList<>();
        List<Captain11Po> updateList = new ArrayList<>();
        List<Captain11Po> captains = jsonArray.toJavaList(Captain11Po.class);

        for (Captain11Po captain : captains) {
            LambdaQueryWrapper<Captain11Po> la = new LambdaQueryWrapper<>();
            la.eq(Captain11Po::getId, captain.getId());
            if (count(la) == 0) {
                addList.add(captain);
            } else {
                updateList.add(captain);
            }
        }

        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
        }

        if (CollectionUtil.isNotEmpty(updateList)) {
            updateBatchById(updateList);
        }
        return true;
    }
}
